package com.neutech.demo;

import com.neutech.entity.Cart;
import com.neutech.entity.Course;
import com.neutech.entity.Student;
import com.neutech.mapper.CartMapper;
import com.neutech.mapper.CourseMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Demo1 {

    public static void main(String[] args) {
        //创建SqlSession工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(
        Demo1.class.getClassLoader().getResourceAsStream("mybatis.xml"));

        SqlSession sqlSession = factory.openSession(true);

//        List<Student > studentList = sqlSession.selectList("sm.listAll");
//        for (Student student : studentList){
//            System.out.println(student);
//        }
//        List<Course> courseList = sqlSession.selectList("cm.listAll");
//        for (Course course : courseList) {
//            System.out.println(course);
//        }
//        int row = sqlSession.update("cm.update");
//        System.out.println(row);
//        sqlSession.commit();
        //getMapper 方法帮你生成实现类对象,在内存中直接生存个类，看不见文件
        CourseMapper courseMapper = sqlSession.getMapper(CourseMapper.class);
//        List<Course> courseList = courseMapper.listAll();
//        for (Course course : courseList) {
//            System.out.println(course);
////        }
        Course course = new Course();
        course.setCno("c011");
        course.setCname("python");
        course.setCredit(1);
        course.setSemester(2);
//        Map<String, Object> map = new HashMap<>();
//        map.put("cno","c012");
//        map.put("cname","jnk");
//        map.put("credit",12);
//        map.put("semester",6);
//        int row = courseMapper.add(course);
//        System.out.println(row);
//        Course course = courseMapper.getByCname("高等数学");
//        System.out.println(course);
        CartMapper cartMapper = sqlSession.getMapper(CartMapper.class);
        for (Cart cart : cartMapper.listAll()) {
            System.out.println(cart);
        }
        sqlSession.close();

    }

}
